package servlet;

import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.*;
import connection.DbConnection;
import java.sql.ResultSet;
import java.util.*;
import model.EmailingSystem;
import model.InternalMail;
import model.PollUserImpl;
import bean.Message;
import bean.PollUser;

public class sendFeedbackServlet extends HttpServlet {
	
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 8275723696814211152L;

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost (request, response);
	}
	
	public void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html");
		response.setCharacterEncoding("GBK");
		
		// Error checking
		System.out.println("doPost method of sendFeedbackServlet reached");
		
		// Get parameters
		String message = request.getParameter("messageTextInput");
		String pollid = request.getSession().getAttribute("selected_poll").toString();
		
		// Error checking
		System.out.println("MEssage: "+message);
		System.out.println("Pollid: "+pollid);
		
		// Create message bean
		Message m = new Message();
		m.setSender(request.getSession().getAttribute("username").toString());
		m.setText(message);

		// Error checking
		System.out.println("Sender: "+m.getSender());
		System.out.println("Message text: "+m.getText());
		
		// Add to database
		DbConnection db = new DbConnection();
		String sql = "Insert into Message (Sender, Text) Values ('"+m.getSender()+"','"+m.getText()+"')";
		System.out.println(sql);
		db.doInsert(sql);
		
		// Get back message id
		String sql2 = "Select max(MessageID) from Message";
		System.out.println(sql2);
		ResultSet rs = db.doSelect(sql2);
		int messageId=-1;
		try {
			while (rs.next()) {
				messageId = rs.getInt(1);
			}
		} catch (Exception e) {
			System.out.println("SQL Exception in sendMessageServlet when getting maxid of messages");
		}
		System.out.println("Messageid: "+messageId);
		
		// Query database for PM's
		String pm;
		String sql4 = "Select PollUser.username from Poll,User_Poll,PollUser where User_Poll.PollID=Poll.ID and User_Poll.Username=PollUser.Username and Poll.ID = '"+pollid+"' AND accessibility=2";
		System.out.println("PM SQL: "+sql4);
		ResultSet rs2 = db.doSelect(sql4);
		try {
			while (rs2.next()) {

				// Set PM
				pm = rs2.getString(1);
				
				// Error checking
				System.out.println("Public message reciever PM: "+pm);
		
				// Add to database receiver list
				String sql7 = "Insert into User_Msg Values('"+pm+"',"+messageId+")";
				System.out.println(sql7);
				db.doInsert(sql7);
			}
		} catch (Exception e) {
			System.out.println("SQL exception in sendMEssageServlet public message section");
		}
		
		response.sendRedirect("Answerform.jsp");
	}
}
